package com.pearlsoft.webapp.rms.resourcesMonitor;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pearlsoft.rms.VcloudManager;
import com.pearlsoft.webapp.core.annotation.QueryHandlerComponent;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Query;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.dataset.SimpleRecord;
import com.pearlsoft.webapp.core.web.basehandler.AbsBaseQueryHandler;
import com.vmware.vcloud.sdk.Organization;
import com.vmware.vcloud.sdk.VCloudException;
import com.vmware.vcloud.sdk.VM;
import com.vmware.vcloud.sdk.Vapp;
import com.vmware.vcloud.sdk.Vdc;
import com.vmware.vcloud.sdk.VirtualDisk;

@QueryHandlerComponent(name = "ResourcesMonitorQueryHandler")
public class ResourcesMonitorQueryHandler extends AbsBaseQueryHandler {

	private SimpleRecord changeVappToRecord(Vapp vapp, String vdcname) {
		SimpleRecord recrod = new SimpleRecord();

		try {
			recrod.setValue("VAPP_VDC", vdcname);
			recrod.setValue("VAPP_NAME", vapp.getResource().getName());
			recrod.setValue("VAPP_DESCRIBE", vapp.getResource()
					.getDescription());
			recrod.setValue("VAPP_STATUS", vapp.getVappStatus().name());
			recrod.setValue("VAPP_VM", vapp.getChildrenVms().size());
			recrod.setValue("VAPP_OWNER", vapp.getResource().getOwner()
					.getUser().getName());
			int cpu = 0;
			int memory = 0;
			for (VM vm : vapp.getChildrenVms()) {
				cpu += vm.getCpu().getNoOfCpus();
				memory += Integer.parseInt(vm.getMemory().getMemorySize()
						.toString());
			}
			recrod.setValue("VAPP_CPU", cpu);
			recrod.setValue("VAPP_MEMORY", memory + " MB");
			recrod.setValue("VAPP_SIZE", vapp.getVappSize() + " MB");

		} catch (VCloudException e) {
			e.printStackTrace();
		}
		return recrod;
	}
	
	/*
	 * 从vm中获取数据并转换成record
	 */
	private Record changeVmToRecord(VM vm,String vApp_Name,String vDC_Name) {
		SimpleRecord recrod = new SimpleRecord();
		try {
			recrod.setValue("VApp_Name", vApp_Name);
			recrod.setValue("VDC_Name", vDC_Name);
			recrod.setValue("VM_ID", vm.getResource().getId());
			recrod.setValue("VM_Name", vm.getResource().getName());
			recrod.setValue("VM_Status", vm.getVMStatus());
			recrod.setValue("VM_Cpu", vm.getCpu().getNoOfCpus());
			recrod
					.setValue("VM_Memory", vm.getMemory().getMemorySize()
							+ " Mb");

			BigInteger diskSize = new BigInteger("0");

			for (VirtualDisk disk : vm.getDisks()) {
				if (disk.isHardDisk()) {

					diskSize = diskSize.add(disk.getHardDiskSize());

				}
			}

			recrod.setValue("VM_HardDisk", diskSize.toString() + " Mb");
		} catch (VCloudException e) {
			e.printStackTrace();
		}
		return recrod;
	}

	@Override
	public void advSearch(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void getEntity(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void quickSearch(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void handler(Query query, DataSet ds) throws Exception {
		VcloudManager vcm = this.getBean("vcloudManager", VcloudManager.class);
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(System.getProperty("ORG_NAME"));		
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
		List<Record> list = new ArrayList<Record>();
		// vdc
		for (Map.Entry<String, Vdc> entry : vdcs.entrySet()) {
			SimpleRecord record = new SimpleRecord();
			Vdc vdc = entry.getValue();
			record.setValue("VDC_Name", vdc.getResource().getName());
			record.setValue("VDC_Desc", vdc.getResource().getDescription());
			Map<String, Vapp> vapps = vcm.getVappMap(vdc);
			List<Map> vappsList = new ArrayList<Map>();
			// vapp
			for (Map.Entry<String, Vapp> vappEntry : vapps.entrySet()) {
				Vapp vapp = vappEntry.getValue();
				SimpleRecord recordVapp = changeVappToRecord(vapp, vdc
						.getResource().getName());
				List<Map> vmList = new ArrayList<Map>();
				List<VM> vms = vcm.getVMList(vapp);
				// vm
				for (VM vm : vms) {
					vmList.add(changeVmToRecord(vm, vapp.getResource().getName(),
							vdc.getResource().getName()).toMap());
				}
				recordVapp.setValue("vmList", vmList);
				vappsList.add(recordVapp.toMap());
			}
			record.setValue("vappsList", vappsList);
			 list.add(record);
		}	
		 ds.setRecords(list);
	}
}
